package com.fairytask.ui.mainUnit
{
	import com.fairytask.data.ViewObject;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.UnitUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	/**
	 * 洗练
	 * @author zhut
	 * 
	 */
	public class RecastUI extends UnitUI
	{
		public var imgBG:MovieClip;			//	背景
		public var img:Clip;
		public var equipName:Label;
		public var equipInfoBG:MovieClip;
		public var recastBtn:Button;
		public var confirmBtn:Button;
		public var leftBG:MovieClip;
		public var rightBG:MovieClip;
		public var leftLab:Label;
		public var rightLab:Label;
		public var leftPropLab:Label;
		public var rightPropLab:Label;
		public var sliverLab:Label;
		public var sliverNum:Label;
		public var equipColor:int;		// 用于存储装备的颜色
		private var _equip:ViewObject;
		
		private var strProp:String;
		private var starBG:Clip;
		private var starMode:Clip;
		private var _point:Point;
		
		private var _sArray:Array	=	[];				// 用于存放以前装备的属性
		private var _nArray:Array 	=	[];				//用于存放洗练后的属性
		private var _propSilver:int;
		private var _sivler	:int;
		private var _name:String;			//装备的名称
		private var _unbind:Array	=	[];				//用于解绑的数组
		private static var _self:RecastUI;
		
		private var _flag:Boolean	=	false;	//用于判断是否有装备
		
		public static function getInstance():RecastUI
		{
			if(_self==null)
			{
				_self=new RecastUI();
			}
			return _self;
		}
		
		public function RecastUI()
		{
			initEvent();
		}
		
		/**
		 * 初始化事件
		 * 
		 */
		private function initEvent():void
		{
			recastBtn.addEventListener(MouseEvent.CLICK,onRecast);
			confirmBtn.addEventListener(MouseEvent.CLICK,onConfirm);
			//绑定装备的等级，进行对银两颜色的显示
			//bindManager.BindViewProp(model.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Silver",getCapital);
		}
		/**
		 *获取玩家拥有的银两 
		 * @param key
		 * @param val
		 * 
		 */
		private function getCapital(key:String,val:int):void
		{
			_propSilver = val;
			if(_sivler!=0)
			{
				sliverNum.htmlStrByBorder=StaticIni.getSwfLangStrVar(_propSilver<_sivler?"1000000037":"1000000034",[_sivler])+StaticIni.getSwfLangStr("1000000067");
			}
			
		}
		
		
		/**
		 * 点击确定触发事件
		 * @param e
		 * 
		 */
		private function onConfirm(e:MouseEvent):void
		{
			if(_flag)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTMMSG_ENSURE_RECAST,_equip.objId);
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PREPARE_RECAST,_equip.objId);
				rightPropLab.htmlStrByBorder="";
			}
			
		}
		
		/**
		 * 点击洗练触发事件
		 * @param e
		 * 
		 */
		private function onRecast(e:MouseEvent):void
		{
			if(_flag)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_RECAST_EQUIP,_equip.objId);
			}
			
		}
		
		/**
		 * 获取原来属性
		 * @param e
		 * 
		 */
		/*public function getProp(e:CustomToClientEvent):void
		{
			getPropLab(e,leftPropLab,2);
		}*/
		
		/*public function getPropLab(evt:CustomToClientEvent,propLab:Label,n:int):void
		{
			sliverNum.visible = true;
			if(starBG!=null)
			{
				starBG.visible = true;
			}
			var pos:int=0;
			strProp="";
			propLab.htmlStrByBorder="";
			if(n==2)
			{
				sliverNum.htmlStrByBorder="";
				_sivler	=	evt.pList[0];
				sliverNum.htmlStrByBorder=StaticIni.getSwfLangStrVar(_propSilver<evt.pList[0]?"1000000037":"1000000034",[evt.pList[0]])+StaticIni.getSwfLangStr(	"1000000067");
			}
			for(var i:int=n;i<evt.pList.length;i+=3)
			{
				addLabelStr(evt.pList[i],evt.pList[i+1],evt.pList[i+2]);	
			}
			propLab.htmlStrByBorder=strProp;
		}*/
		
		//获取装备星级
		public function setEquip(equip:ViewObject,viewID:int):void
		{
			if(equip == null)
			{
				return;
			}
			if(_point==equip.objId)
			{
				return;
			}
			_point = equip.objId;
			leftPropLab.htmlStrByBorder = "";
			rightPropLab.htmlStrByBorder= "";
			sliverNum.htmlStrByBorder = "";
			_flag	=	true;
			if(equip==null)
			{
				return;
			}
			_equip=equip;
			_unbind.push([equip,"ConfigID",bindManager.BindPropInViewObj(model.scene.GetView(viewID),_equip.ObjKey,"ConfigID",onAdd,null)]);
			_unbind.push([equip,"Star",bindManager.BindPropInViewObj(model.scene.GetView(viewID),_equip.ObjKey,"Star",onAdd,null)]);
			_unbind.push([equip,"Color",bindManager.BindPropInViewObj(model.scene.GetView(viewID),_equip.ObjKey,"Color",onAdd,null)]);
			//绑定装备的等级，进行对名字显示
			_unbind.push([equip,"Level",bindManager.BindPropInViewObj(model.scene.GetView(viewID),_equip.ObjKey,"Level",onAddColor,null)]);
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PREPARE_RECAST,equip.objId);
		}
		
		private function onAddColor(key:String,val:int):void
		{
			var headName:String	=	"";
			//headName	=	TipsTools.getRefineName(val) +_name;
			//equipName.htmlStrByBorder = TipsTools.onlyColor(headName,equipColor);
		}

		
		/**
		 *根据不同的属性显示不同的数字 
		 * @param key
		 * @param val
		 * 
		 */		
		private function onAdd(key:String,val:int):void
		{
			switch(key)
			{
				case "Star":
					initInfo(val);
					break;
				case "ConfigID":
					img.source=ResConfig.iconPath+(StaticIni.getIniVar(val,"Icon"))+ResConfig.iconImg;
					//添加tips
					img.addEventListener(MouseEvent.ROLL_OVER,function(evt:MouseEvent):void
					{
						TipsManage.ShowItemMaxTips(val,_equip);
					});
					img.addEventListener(MouseEvent.ROLL_OUT,function(evt:MouseEvent):void
					{
						ToolTips.hide();
					});
					_name = StaticIni.getIniVar(val,"Name");
					break;
				case "Color":
					equipColor	= val;
					break;
				default:
					break;
			}
				
			
		}
		
		private function initInfo(num:int):void
		{
			if(starBG!=null)
			{
				windowBG.removeChild(starBG);
			}
			starBG=new Clip();
			starBG.visible = false;
			starBG.x=30;
			starBG.y=100;
			starBG.height=80;
			starBG.width=200;
			windowBG.addChild(starBG);
			for(var i:int=1;i<=num;i++)
			{
				createStarMode(i);
			}
			
		}
		
		private function createStarMode(i:int):void
		{
			var row:int;
			var col:int;
			if(i%10!=0)
			{
				row=int(i/10)+1;
			}
			else
			{
				row=int(i/10);
			}
			if(i%10!=0)
			{
				col=int(i%10);
			}
			else
			{
				col=10
			}
			starMode=new Clip();
			starMode.source=ResConfig.pngResPath+"tips_star2.png";
			starMode.width = 23;
			starMode.height = 23;
			starMode.x=(col-1)*25;
			starMode.y=(row-1)*25;
			starBG.addChild(starMode);
		}
		public function clear():void
		{
			if(starBG != null)
				starBG.visible = false;
			leftPropLab.htmlStrByBorder = "";
			rightPropLab.htmlStrByBorder= "";
			//sliverLab.htmlStrByBorder   = "";
			sliverNum.htmlStrByBorder   = "";
			img.clear();
			equipName.htmlStrByBorder="";
			sliverNum.visible	=	false;
			_point = null;
			for each(var itemBind:Array in _unbind)
			{
				bindManager.UnbindProp(itemBind.shift(),itemBind.shift(),itemBind.shift());
			}
			_flag = false;
		}
		
		private function addLabelStr(col:int,name:String,val:int):void
		{
			var str:String;
			//str=TipsTools.onlyColor(StaticIni.getSwfLangStrVar(name,[val]),col);
			strProp+=str+"<br>";
		}
		
	}
}